package cn.com.pagoda.ideplugin.service;

import cn.com.pagoda.ideplugin.common.transcoder.ForwardTranscodeFunction;
import cn.com.pagoda.ideplugin.common.transcoder.ReverseTranscodeFunction;
import cn.com.pagoda.ideplugin.common.transcoder.TranscoderAlgorithmEnum;
import cn.com.pagoda.ideplugin.common.transcoder.TranscoderTypeEnum;

import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

/**
 * @Author xieluxin
 * @Date 2023/7/8 22:34
 * @Version 1.0
 */
public class TranscoderService {

    /**
     * 将明文进行编码
     * @param plaintext
     * @param algorithmEnum
     * @return
     */
    public String encodeByContent(String plaintext, TranscoderAlgorithmEnum algorithmEnum){
        if(!algorithmEnum.getTranscoderTypeEnum().equals(TranscoderTypeEnum.ENCODE_AND_DECODE_SUB_PANEL)){
            throw new RuntimeException("仅支持类型是编码/解码的算法");
        }
        ForwardTranscodeFunction transcodeFunction = algorithmEnum.getForwardTranscodeFunction();
        try {
            return transcodeFunction.forwardTranscode(plaintext,null);
        } catch (Exception e) {
            System.out.println("编码失败 errMsg="+e.getMessage());
            throw new RuntimeException(e);
        }
    }
    /**
     * 将文本进行解码, 还原为明文
     * @param ciphertext
     * @param algorithmEnum
     * @return
     */
    public String decodeByContent(String ciphertext, TranscoderAlgorithmEnum algorithmEnum){
        if(!algorithmEnum.getTranscoderTypeEnum().equals(TranscoderTypeEnum.ENCODE_AND_DECODE_SUB_PANEL)){
            throw new RuntimeException("仅支持类型是编码/解码的算法");
        }
        ReverseTranscodeFunction reverseTranscodeFunction = algorithmEnum.getReverseTranscodeFunction();
        try {
            return reverseTranscodeFunction.reverseTranscode(ciphertext,null);
        } catch (Exception e) {
            System.out.println("解码失败 errMsg="+e.getMessage());
            throw new RuntimeException(e);
        }
    }


    /**
     * 将明文进行加密
     * @param plaintext
     * @param algorithmEnum
     * @return
     */
    public String encryptByContent(String plaintext,String key, TranscoderAlgorithmEnum algorithmEnum){
        if(!algorithmEnum.getTranscoderTypeEnum().equals(TranscoderTypeEnum.ENCRYPT_AND_DECRYPT_SUB_PANEL)){
            throw new RuntimeException("仅支持类型是加密/解密的算法");
        }
        ForwardTranscodeFunction transcodeFunction = algorithmEnum.getForwardTranscodeFunction();
        try {
            return transcodeFunction.forwardTranscode(plaintext,key);
        } catch (Exception e) {
            System.out.println("加密失败 errMsg="+e.getMessage());
            throw new RuntimeException(e);
        }
    }
    /**
     * 将密文进行解密, 还原为明文
     * @param ciphertext
     * @param algorithmEnum
     * @return
     */
    public String decryptByContent(String ciphertext,String key, TranscoderAlgorithmEnum algorithmEnum){
        if(!algorithmEnum.getTranscoderTypeEnum().equals(TranscoderTypeEnum.ENCRYPT_AND_DECRYPT_SUB_PANEL)){
            throw new RuntimeException("仅支持类型是加密/解密的算法");
        }
        ReverseTranscodeFunction reverseTranscodeFunction = algorithmEnum.getReverseTranscodeFunction();
        try {
            return reverseTranscodeFunction.reverseTranscode(ciphertext,key);
        } catch (Exception e) {
            System.out.println("加密失败 errMsg="+e.getMessage());
            throw new RuntimeException(e);
        }
    }

    /**
     * 对明文进行hash计算
     * @param plaintext
     * @param algorithmEnum
     * @return
     */
    public String calculateHashByContent(String plaintext,String key, TranscoderAlgorithmEnum algorithmEnum){
        if(!algorithmEnum.getTranscoderTypeEnum().equals(TranscoderTypeEnum.HASH_CALCULATE_SUB_PANEL)){
            throw new RuntimeException("仅支持类型是Hash计算的算法");
        }
        ForwardTranscodeFunction reverseTranscodeFunction = algorithmEnum.getForwardTranscodeFunction();
        try {
            return reverseTranscodeFunction.forwardTranscode(plaintext,key);
        } catch (Exception e) {
            System.out.println("计算hash值失败 errMsg="+e.getMessage());
            throw new RuntimeException(e);
        }
    }
}